package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-22
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService videoService;  //小节的service

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id查询课程里面的小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> videoVideoWrapper = videoService.list(videoQueryWrapper);
//创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3.遍历查询章节list集合进行封装
        for(int i =0;i < eduChapterList.size();i++){
            //每个章节
            EduChapter   chatper = eduChapterList.get(i);
        //eduChapter对象值复制到ChapterVo里面去
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chatper,chapterVo);//id,title
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);//finalList放的每个章节的id和title。

            //创建集合，用于封装章节中的小节
            List<VideoVo> videoList= new ArrayList<>();

            //4.遍历查询小节list集合，进行封装
            for(int j=0;j<videoVideoWrapper.size();j++){
                EduVideo eduVideo = videoVideoWrapper.get(j);
                VideoVo videoVo = new VideoVo();
                //判断小节里面的chapterid和章节里面的id是否一样
                if(eduVideo.getChapterId().equals(chatper.getId())){
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            }
            //把封装之后小节list集合，放到章节对象里面
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }

    /**
     * 删除章节的方法
     * 章节有小节，就删除；没小节，就不删除
     * @param chapterId
     */
    @Override
    public boolean deleleChapter(String chapterId) {
        //根据chapterId章节id查询小节表，如果查询数据，不进行删除。
       QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
       wrapper.eq("chapter_id",chapterId);//select * from  VideoChapter where chapter_id =?
        int count = videoService.count(wrapper);
     /*   if(count>0){
            //能查询出来小节，不进行删除
            System.out.println("不能删除");
            return 0;
        }else{*/
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }

        //根据课程id删除章节
    @Override
    public void removeVideoByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapper= new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

}

